import libtcodpy as libtcod

try:
    import pygame
    NO_SOUND = False
except ImportError:
    NO_SOUND = True

import splash
import menu
import utils
import save_quit  # move into here

def help(console, player):
    text = """
 Movement: <Arrow Keys>  |  Kill the monsters in the Iron 
 Inventory: i            |  Prison before they reach the
 Equip weapon: e         |  bottom.  Watch out for the 
 Wear Armor: w           |  chompers.  Party every night,
 Quit: <ESC>             |  sleep all day.
"""
    lines = text.split('\n')
    width = max([len(line) for line in lines]) + 2
    height = len(lines) + 2
    help = splash.Splash(text, width, height, border=True, title='Help', background_alpha=0.8)
    x, y = (console.width - width) / 2, (console.height - height) / 2

    if not NO_SOUND: pygame.mixer.pause()
    help.show(console, x, y)
    if not NO_SOUND: pygame.mixer.unpause()

def inventory(console, player):
    weapons = [player.weapon]
    armor = [player.armor]
    other = []

    for item in player.inventory:
        if hasattr(item, 'weapon'):
            weapons.append(item)
        elif hasattr(item, 'armor'):
            armor.append(item)
        else:
            other.append(item)

    # Efficiency is for the efficient.
    items = sorted(weapons) + sorted(armor) + sorted(other)
    footer = "Gold: %d, Fame: %d" % (player.gold, player.fame)
    selector = utils.ItemSelector(items, title='Inventory', footer=footer)

    selector.show(console)

def wield(console, player):
    items = [player.weapon] + [item for item in player.inventory if hasattr(item, 'weapon')]
    items.sort()
    selector = utils.ItemSelector(items, title='Select weapon to wield')

    item = selector.show(console)

    if item is not None:
        player.wield(item)

def wear(console, player):
    items = [player.armor] + [item for item in player.inventory if hasattr(item, 'armor')]
    items.sort()
    selector = utils.ItemSelector(items, title='Select armor to wear')

    item = selector.show(console)

    if item is not None:
        player.wear(item)

def handle_keys(root, player, jukebox):
    key = root.check_for_keypress(libtcod.KEY_PRESSED)

    if key.vk == libtcod.KEY_ESCAPE:
        save_quit.exit('Quit', (0, 0), jukebox.event_song('exit'))(root, player)
        return
    elif key.vk == libtcod.KEY_NONE:
        return

    char = chr(key.c)
    if key.vk in (libtcod.KEY_KP8, libtcod.KEY_UP):
        if not player.cool_down:
            return (0, -1)
    elif key.vk in (libtcod.KEY_KP2, libtcod.KEY_DOWN):
        if not player.cool_down:
            return (0, 1)
    elif key.vk in (libtcod.KEY_KP4, libtcod.KEY_LEFT):
        if not player.cool_down:
            return (-1, 0)
    elif key.vk in (libtcod.KEY_KP6, libtcod.KEY_RIGHT):
        if not player.cool_down:
            return (1, 0)
    elif key.vk in (libtcod.KEY_KP9, libtcod.KEY_PAGEUP):
        if not player.cool_down:
            return (1, -1)
    elif key.vk in (libtcod.KEY_KP7, libtcod.KEY_HOME):
        if not player.cool_down:
            return (-1, -1)
    elif key.vk in (libtcod.KEY_KP1, libtcod.KEY_END):
        if not player.cool_down:
            return (-1, 1)
    elif key.vk in (libtcod.KEY_KP3, libtcod.KEY_PAGEDOWN):
        if not player.cool_down:
            return (1, 1)
    elif key.vk == libtcod.KEY_PRINTSCREEN:
        libtcod.sys_save_screenshot()
    elif char == '?' or key.vk == libtcod.KEY_F1:
        ACTIONS['help'](root, player)
    elif char in ('i', 'I'):
        ACTIONS['inventory'](root, player)
    elif char in ('e', 'E'):
        ACTIONS['wield'](root, player)
        jukebox.event('equip')
    elif char in ('w', 'W'):
        ACTIONS['wear'](root, player)
        jukebox.event('equip')
#    elif char == '$':
#        player.gold += 500
#        print "You now have %d GP" % player.gold


ACTIONS = dict(help=help,
               inventory=inventory,
               wield=wield,
               wear=wear,
              ) 

